using System;
using System.Collections.Generic;
using MVP.Demo.Core;
using MVP.Demo.Model;
using MVP.Demo.View;

namespace MVP.Demo.Presentation
{
    public class PlayerListPresenter
    {
        private IPlayerRepository playerRepository;
        private IPlayerListView view;

        public PlayerListPresenter(IPlayerRepository playerRepository, IPlayerListView view)
        {
            this.playerRepository = playerRepository;
            this.view = view;
        }

        public PlayerListPresenter(IPlayerListView view) : this(new PlayerRepository(), view)
        {
        }

        public void InitializeView()
        {
            view.SavePlayer += delegate { SaveNewPlayer(); };

            if (!view.IsPagePostBack)
            {
                LoadPlayersToView();
            }
        }

        private void LoadPlayersToView()
        {
            view.ListOfPlayers = playerRepository.GetPlayersForTeam(view.TeamID);
        }

        public void SaveNewPlayer()
        {
            playerRepository.SavePlayer(view.FirstName, view.LastName, view.TeamID);

            LoadPlayersToView();
        }

        public void DeletePlayer(string playerID)
        {
            playerRepository.DeletePlayer(playerID);

            LoadPlayersToView();
        }
    }
}
